/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DatabaseClientUI.java
 *
 * Created on Apr 2, 2012, 3:19:15 PM
 */
package databaseclient;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import objects.FileInfo;
import objects.UserInfo;
import services.*;

/**
 *
 * @author tinvukhac
 */
public class DatabaseClientUI extends javax.swing.JFrame {

    private String userId;
    private String serverIp;
    private DefaultTableModel myFilesModel;
    private ArrayList<MyFile> myFiles = new ArrayList<MyFile>();
    private ShareManager shareManager;
    private XmlManager xmlManager;
    private ConfirmThread confirmThread;
    private int port;
    private List<Integer> networkFileIndex;
    private SharingDialog sharingDialog;

    public DatabaseClientUI(String userId, String serverIp, int port) {
        try {
            initComponents();

            //set properties of this user
            this.userId = userId;
            this.serverIp = serverIp;
            this.port = port;
            this.setTitle(userId);

            //initial one thread for confirm online to server
            confirmThread = new ConfirmThread(userId, serverIp, 5000);
            confirmThread.start();
            xmlManager = new XmlManager(userId);

            //update current works of user
            updateMyFiles();
            shareManager = new ShareManager(jTable1, myFiles, xmlManager, port, userId, serverIp);

            //update sharing file from server. 
            addSelectionRowNetworkFile();
            updateNetworkFiles();
        } catch (Exception e) {
            System.out.println("constructor in DatabaseClientUI");
            e.printStackTrace();
        }
    }

    /**
     * Creates new form DatabaseClientUI
     */
    public DatabaseClientUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabPanel = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane7 = new javax.swing.JScrollPane();
        jTable5 = new javax.swing.JTable();
        txtTextToSearch = new javax.swing.JTextField();
        /*
        btnSearch = new javax.swing.JButton();
        */ 
        java.net.URL searchImageURL   = this.getClass().getClassLoader().getResource("image/search.png");
        ImageIcon search = new ImageIcon(searchImageURL);
        btnSearch=new JButton(search);
        /*
        jButton6 = new javax.swing.JButton();
        */
        java.net.URL downloadImageURL   = this.getClass().getClassLoader().getResource("image/download.png");
        ImageIcon download = new ImageIcon(downloadImageURL);
        jButton6=new JButton(download);
        /*
        jButton7 = new javax.swing.JButton();
        */
        java.net.URL shareImageURL   = this.getClass().getClassLoader().getResource("image/upload.png");
        ImageIcon share = new ImageIcon(shareImageURL);
        jButton7=new JButton(share);
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel7 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jPanel8 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jPanel9 = new javax.swing.JPanel();
        jScrollPane6 = new javax.swing.JScrollPane();
        jTable3 = new javax.swing.JTable();
        /*
        jButton2 = new javax.swing.JButton();
        */
        java.net.URL refreshImageURL   = this.getClass().getClassLoader().getResource("image/refresh.png");
        ImageIcon refresh = new ImageIcon(refreshImageURL);
        jButton2=new JButton(refresh);
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        exitMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        contentsMenuItem = new javax.swing.JMenuItem();
        aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "#", "Name", "Size(KB)", "Download Speed", "Upload Speed", "status"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(jTable1);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 618, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 185, Short.MAX_VALUE)
        );

        jButton1.setText("jButton1");

        jButton3.setText("jButton3");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 622, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addGap(34, 34, 34)
                        .addComponent(jButton3)
                        .addGap(48, 48, 48)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(19, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        tabPanel.addTab("My files", jPanel2);

        jTable5.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "#", "File Name", "File Size", "Sharing", "Downloading"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane7.setViewportView(jTable5);

        txtTextToSearch.setToolTipText("Search File");
        txtTextToSearch.setPreferredSize(new java.awt.Dimension(199, 28));

        btnSearch.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btnSearch.setPreferredSize(new java.awt.Dimension(40, 30));
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        jButton6.setText("Download");
        jButton6.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jButton6.setPreferredSize(new java.awt.Dimension(100, 30));
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton7.setText("Share");
        jButton7.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jButton7.setPreferredSize(new java.awt.Dimension(90, 30));
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jLabel3.setText("File Name:");
        jLabel3.setName("");

        jLabel4.setText("      ");

        jLabel5.setText("File Size:");

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setRows(5);
        jScrollPane4.setViewportView(jTextArea1);

        jLabel6.setText("Description:");

        jLabel7.setText("Orginal User:");

        jLabel8.setText("           ");

        jLabel9.setText("               ");

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5))
                .addGap(3, 3, 3)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 404, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 506, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(39, 39, 39)
                        .addComponent(jLabel7)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(32, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jLabel8))
                        .addGap(32, 32, 32)
                        .addComponent(jLabel6)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addGap(0, 27, Short.MAX_VALUE)
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9))
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Info", jPanel7);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "#", "User Name", "Ip", "Port"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane5.setViewportView(jTable2);

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 608, Short.MAX_VALUE)
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Sharing List", jPanel8);

        jTable3.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "#", "User Name", "Ip", "Port"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                true, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane6.setViewportView(jTable3);

        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 608, Short.MAX_VALUE)
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Downloading List", jPanel9);

        jButton2.setText("Refresh");
        jButton2.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jButton2.setPreferredSize(new java.awt.Dimension(100, 30));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane7, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                        .addGap(19, 19, 19))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(txtTextToSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtTextToSearch, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane7, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );

        jTabbedPane1.getAccessibleContext().setAccessibleName("");

        tabPanel.addTab("Network files", jPanel6);

        fileMenu.setText("File");

        jMenuItem1.setText("Acount profile");
        fileMenu.add(jMenuItem1);

        jMenu2.setText("Log out");
        fileMenu.add(jMenu2);

        exitMenuItem.setText("Exit");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText("Help");

        contentsMenuItem.setText("Support");
        helpMenu.add(contentsMenuItem);

        aboutMenuItem.setText("About");
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 647, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabPanel)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        System.exit(0);
    }//GEN-LAST:event_exitMenuItemActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        updateNetworkFiles();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here:
        int rowIndex = jTable5.getSelectedRow();
        if (rowIndex != -1) {
            int fileId = networkFileIndex.get(rowIndex);

            List<UserInfo> seedingUsers = new ArrayList<UserInfo>();
            FileInfo fileInfo;
            FileServices fs = new FileServices();

            seedingUsers = fs.getFileSeeders(fileId, serverIp);
            fileInfo = fs.getFileInfo(fileId, serverIp);

            try {
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                int approve = fileChooser.showOpenDialog(this);
                if (approve == JFileChooser.APPROVE_OPTION) {
                    File directory = fileChooser.getSelectedFile();
                    String absoluteFileName = directory.getAbsolutePath() + "\\" + fileInfo.getFileName();

                    MyFile newFile = new MyFile(fileInfo);
                    newFile.setPathName(absoluteFileName);

                    //confirm downloading to server

                    fs.confirmDownloading(fileId, serverIp, userId);

                    xmlManager.addNodeXML(newFile);
                    shareManager.AddDownloadFile(newFile, seedingUsers);
                    tabPanel.setSelectedIndex(0);
                }
            } catch (Exception e) {
                System.out.println("btnDownloadActionPerformed in DatabaseClientUI");
                e.printStackTrace();
            }
        }

    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:
        sharingDialog = new SharingDialog();

        sharingDialog.addShareListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                FileInfo file;

                //check for text field for file name null
                if (sharingDialog.getFileNameField().toString().length() == 0) {
                    JOptionPane.showMessageDialog(null, "File path is invalid", "Error", 0);
                    sharingDialog.getDescriptionAread().setText("");
                    return;
                }
                String s = sharingDialog.getFileNameField().getText();
                File newSharingFile = new File(s);
                //check for file name is invalidate
                if (newSharingFile.exists() == false) {
                    JOptionPane.showMessageDialog(null, "File path is invalid", "Error", 0);
                    sharingDialog.getFileNameField().setText("");
                    sharingDialog.getDescriptionAread().setText("");
                    return;
                }

                // Create new FileInfo object
                if (sharingDialog.getDescriptionAread().getText().equals("") == false) {
                    file = new FileInfo(userId, newSharingFile.getName(), (float) newSharingFile.length(),
                            sharingDialog.getDescriptionAread().getText());
                } else {
                    file = new FileInfo(userId, newSharingFile.getName(), (float) newSharingFile.length(),
                            " ");
                }

                // Send file information to server
                FileServices fs = new FileServices();
                int fileId = fs.share(serverIp, userId, file);
                if (fileId != -1) {
                    MyFile newFile = new MyFile(file);
                    newFile.setIsComplete(true);
                    newFile.setCompleteData(newFile.getFileSize());
                    newFile.setFileId(fileId);
                    newFile.setPathName(newSharingFile.getAbsolutePath());

                    xmlManager.addNodeXML(newFile);
                    shareManager.AddSharedFile(newFile);

                    JOptionPane.showMessageDialog(null, "Share file successful", "Information", 1);
                } else {
                    JOptionPane.showMessageDialog(null, "Share file error", "Information", 1);
                }
                sharingDialog.getFileNameField().setText("");
                sharingDialog.getDescriptionAread().setText("");
            }
        });

        sharingDialog.setVisible(true);
    }//GEN-LAST:event_jButton7ActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        // TODO add your handling code here:
        String pattern = txtTextToSearch.getText().trim();
        if (pattern.length() == 0) {
            JOptionPane.showMessageDialog(null, "Please enter text to search", "Invalid", 0);
        } else {
            List<FileInfo> list = new ArrayList<FileInfo>();
            List<Integer> networkFileIndex = new ArrayList<Integer>();
            FileServices fs = new FileServices();

            DefaultTableModel model = (DefaultTableModel) jTable5.getModel();
            model.setNumRows(0);

            list = fs.searchFile(pattern, true, serverIp);

            for (int i = 0; i < list.size(); ++i) {
                FileInfo temp = list.get(i);
                Integer fileId = new Integer(temp.getFileId());
                AccountServices as = new AccountServices();
                int numberSharing = as.getNumberSharing(fileId.intValue(), serverIp);
                int numberDownloading = as.getNumberDownloading(fileId.intValue(), serverIp);

                model.addRow(new Object[]{
                            i + 1, temp.getFileName(), Ultility.convertMem(temp.getFileSize()),
                            numberSharing, numberDownloading
                        });

                networkFileIndex.add(fileId);
            }

            this.networkFileIndex = networkFileIndex;
        }
    }//GEN-LAST:event_btnSearchActionPerformed

    public void updateNetworkFiles() {

        List<FileInfo> list = new ArrayList<FileInfo>();
        List<Integer> networkFileIndex = new ArrayList<Integer>();
        FileServices fs = new FileServices();

        DefaultTableModel model = (DefaultTableModel) jTable5.getModel();
        model.setNumRows(0);

        list = fs.getSharedFileList(serverIp);

        for (int i = 0; i < list.size(); ++i) {
            FileInfo temp = list.get(i);
            Integer fileId = new Integer(temp.getFileId());
            AccountServices as = new AccountServices();
            int numberSharing = as.getNumberSharing(fileId.intValue(), serverIp);
            int numberDownloading = as.getNumberDownloading(fileId.intValue(), serverIp);

            model.addRow(new Object[]{
                        i + 1, temp.getFileName(), Ultility.convertMem(temp.getFileSize()),
                        numberSharing, numberDownloading
                    });

            networkFileIndex.add(fileId);
        }

        this.networkFileIndex = networkFileIndex;

    }

    public void addSelectionRowNetworkFile() {
        jTable5.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                int index = jTable5.getSelectedRow();
                if (index < 0) {
                    return;
                }
                int fileId = networkFileIndex.get(index);
                FileServices fs = new FileServices();
                FileInfo tempInfo = fs.getFileInfo(fileId, serverIp);
                jLabel4.setText(tempInfo.getFileName());
                jLabel8.setText(String.valueOf(tempInfo.getFileSize()) + "B");
                jLabel9.setText(tempInfo.getUserId());
                jTextArea1.setText(tempInfo.getFileDescription());

                DefaultTableModel sharingModel = (DefaultTableModel) jTable2.getModel();
                sharingModel.setRowCount(0);
                List<UserInfo> sharingList = fs.getSharingUser(fileId, serverIp);
                for (int i = 0; i < sharingList.size(); ++i) {
                    UserInfo tempUserInfo = sharingList.get(i);
                    sharingModel.addRow(new Object[]{
                                i + 1, tempUserInfo.getUserId(), tempUserInfo.getUserAddress(), tempUserInfo.getPort()
                            });
                }

                DefaultTableModel downloadingModel = (DefaultTableModel) jTable3.getModel();
                downloadingModel.setRowCount(0);
                List<UserInfo> downloadingList = fs.getDownloadingUser(fileId, serverIp);
                for (int i = 0; i < downloadingList.size(); ++i) {
                    UserInfo tempUserInfo = downloadingList.get(i);
                    downloadingModel.addRow(new Object[]{
                                i + 1, tempUserInfo.getUserId(), tempUserInfo.getUserAddress(), tempUserInfo.getPort()
                            });
                }
            }
        });
    }

    public void updateMyFiles() {
        myFilesModel = (DefaultTableModel) jTable1.getModel();
        myFiles = xmlManager.scanMyFile();
        int length = myFiles.size();
        if (length != 0) {
            for (int i = 0; i < length; ++i) {
                DecimalFormat form = new DecimalFormat("0.00");
                float percent = (myFiles.get(i).getCompleteData() / myFiles.get(i).getFileSize()) * 100;

                myFilesModel.addRow(new Object[]{
                            i + 1, myFiles.get(i).getFileName(), Ultility.convertMem(myFiles.get(i).getFileSize()),
                            "0B/s", "0B/s", form.format(percent) + "%"
                        });
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new DatabaseClientUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JButton btnSearch;
    private javax.swing.JMenuItem contentsMenuItem;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JScrollPane jScrollPane7;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jTable3;
    private javax.swing.JTable jTable5;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JTabbedPane tabPanel;
    private javax.swing.JTextField txtTextToSearch;
    // End of variables declaration//GEN-END:variables
}
